package com.person.android.widgets;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.person.android.formatts.R;

/**
 * @autor zhaokang.
 * @time on 2015/12/19.
 */
public class FeedbackDialog extends Dialog {

    public FeedbackDialog(Context context) {
        super(context, 0);
    }

    public FeedbackDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static final class Builder {

        private int titleID;
        private int messageID;
        private Context mContext;
        private int positiveBtnTxt;
        private DialogInterface.OnClickListener positiveButtonClickListener;

        public Builder(Context context) {
            this.mContext = context;
        }

        public FeedbackDialog create(){
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final FeedbackDialog dialog = new FeedbackDialog(mContext, R.style.feedback_dialog_style);
            View view = inflater.inflate(R.layout.feedback_dialog_layout, null);
            dialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            if (titleID != 0) {
                ((TextView) view.findViewById(R.id.title)).setText(titleID);
            }
            if (positiveBtnTxt != 0){
                ((Button) view.findViewById(R.id.btn_ok)).setText(positiveBtnTxt);
            }
            if (positiveButtonClickListener != null) {
                (view.findViewById(R.id.btn_ok)).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                });
            }
            dialog.setContentView(view);
            return dialog;
        };

        public Builder setTitleId(int titleID) {
            this.titleID = titleID;
            return this;
        }

        public Builder setMessageId(int messageID) {
            this.messageID = messageID;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveBtnTxt = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
    }
}
